﻿using System;

namespace ProblemsSet
{
    public class Problem_134 : BaseProblem
    {
        public override object GetResult()
        {
            const int max = 1000000;
            //const int max = 100;


            var lst = MathLogic.GetPrimeList(11*max/10, true);

            var res = "";

            for (var i = 0; i < lst.Count-1; i++)
            {
                var p1 = lst[i];
                if (p1 < 5) continue;
                if (p1 > max) break;
                var p2 = lst[i + 1];
                var cnt = (long)Math.Log10(p1) + 1;
                var init = (long) Math.Pow(10, cnt);
                var start = init/p2;
                var rs = (ulong)p2*(ulong)start;
                while(rs%(ulong)init  != (ulong)p1)
                {
                    rs += (ulong)p2;
                }
                res = MathLogic.SummString(res, rs.ToString());
            }
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that 1219 is the smallest number such that the last digits are formed by p1 whilst also being divisible by p2.

In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive primes, p2  p1, there exist values of n for which the last digits are formed by p1 and n is divisible by p2. Let S be the smallest of these values of n.

Find  S for every pair of consecutive primes with 5  p1  1000000.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return "18613426663617118";
            }
        }
    }
}
